//alert - success - error - warning - information - confirmation
var DEFAULT_NOTY_LAYOUT = 'topCenter';
var DEFAULT_TIMOUT = 3000;

var container_notyObj
function msgBox(obj , type , msg , timeout){
	$.noty.clearQueue();
	$.noty.closeAll();
	if(typeof(timeout) != 'undefined') {
		timeout = DEFAULT_TIMOUT;
	}
	if(container_notyObj != null && container_notyObj != undefined && typeof(container_notyObj) != 'undefined'   ){
		container_notyObj.setText(msg);
		container_notyObj.setType(type);
		return;
	}
	if(timeout == undefined) {
		timeout = DEFAULT_TIMOUT;
	}
	container_notyObj = obj.noty({
		text        : msg,
		type        : type,
		dismissQueue: false,
		layout      : 'inline',
		timeout		: timeout,
		theme       : 'defaultTheme',
		killer:true,
		maxVisible  : 1,
		callback: {
			afterClose: function() {container_notyObj = null;}
		}
	});
}




var noty_container_Obj;
(function ($) {
	$.fn.extend({
		info: function (options) {
			//参数和默认值
			var defaults = {
				msg: '',
				timeout:DEFAULT_TIMOUT
			};
			var options = $.extend(defaults, options);
			return this.each(function () {
				if(noty_container_Obj != null && noty_container_Obj != undefined && typeof(noty_container_Obj) != 'undefined'   ){
					noty_container_Obj.setText(options.msg);
					noty_container_Obj.setType('information');
					return;
				}
				noty_container_Obj = $(this).noty({
					text        : options.msg,
					type        : 'information',
					dismissQueue: false,
					layout      : 'inline',
					timeout		: options.timeout,
					theme       : 'defaultTheme',
					killer:true,
					maxVisible  : 1,
					callback: {

						afterClose: function() {noty_container_Obj = null;}
					}
				});
			});
		}
	});
})(jQuery);

(function ($) {
	$.fn.extend({
		succ: function (options) {
			//参数和默认值
			var defaults = {
				msg: '',
				timeout:DEFAULT_TIMOUT
			};
			var options = $.extend(defaults, options);
			return this.each(function () {
				$.noty.clearQueue();
				$.noty.closeAll();
				if(noty_container_Obj != null && noty_container_Obj != undefined && typeof(noty_container_Obj) != 'undefined'   ){
					noty_container_Obj.setText(options.msg);
					noty_container_Obj.setType('success');
					return;
				}
				noty_container_Obj = $(this).noty({
					text        : options.msg,
					type        : 'success',
					dismissQueue: false,
					layout      : 'inline',
					timeout		: options.timeout,
					theme       : 'defaultTheme',
					killer:true,
					maxVisible  : 1,
					callback: {
						afterClose: function() {noty_container_Obj = null;}
					}
				});
			});
		}
	});
})(jQuery);

(function ($) {
	$.fn.extend({
		error: function (options) {
			//参数和默认值
			var defaults = {
				msg: '',
				timeout:DEFAULT_TIMOUT
			};
			var options = $.extend(defaults, options);
			return this.each(function () {
				$.noty.clearQueue();
				$.noty.closeAll();
				if(noty_container_Obj != null && noty_container_Obj != undefined && typeof(noty_container_Obj) != 'undefined'   ){
					noty_container_Obj.setText(options.msg);
					noty_container_Obj.setType('error');
					return;
				}
				noty_container_Obj = $(this).noty({
					text        : options.msg,
					type        : 'error',
					dismissQueue: false,
					layout      : 'inline',
					timeout		: options.timeout,
					theme       : 'defaultTheme',
					killer:true,
					maxVisible  : 1,
					callback: {
						afterClose: function() {noty_container_Obj = null;}
					}
				});

				//msgBox( $(this), 'error' , options.msg , options.timeout);
			});
		}
	});
})(jQuery);

/*
 生成提示消息
 */
var notyObj;
function genMsg(msg , type, notyLayout , timeout , callback){
	var template = '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>';


	if(type == 'loading') {
		type = 'information';
		template = '<div class="noty_message"><i class="ace-icon fa fa-refresh icon-refresh icon-spin"></i>&nbsp;<span class="noty_text"></span><div class="noty_close"></div></div>';
	}

	if(notyObj != null && notyObj != undefined && typeof(notyObj) != 'undefined' && notyObj.options.type != 'information'  ){
		notyObj.setText(msg);
		notyObj.setType(type);
		return;
	}

	if(callback == null || callback == undefined || typeof(callback) == 'undefined'   ){
		callback = function(){notyObj = null;};
	}

	notyObj = noty({
		layout: notyLayout,
		text: msg,
		type: type,
		dismissQueue: false,
		killer:true,
		maxVisible  : 1,
		force: false,
		timeout: timeout,
		theme: 'defaultTheme',
		template    : template,
		callback: {
			onShow: function() {},
			afterShow:  function() {},
			onClose: function() {},
			afterClose: function(){callback();}
		}
	});
}



/*
 function alert(msg,notyLayout) {
 if(notyLayout == undefined) {
 notyLayout = DEFAULT_NOTY_LAYOUT;
 }
 genMsg(msg , 'alert' , notyLayout , DEFAULT_TIMOUT);
 }
 */

function info(msg,notyLayout) {
	if(notyLayout === undefined) {
		notyLayout = DEFAULT_NOTY_LAYOUT;
	}
	genMsg(msg , 'information' , notyLayout , DEFAULT_TIMOUT);
}

function succ(msg , callback ,notyLayout) {
	if(notyLayout == null || notyLayout === undefined) {
		notyLayout = DEFAULT_NOTY_LAYOUT;
	}
	genMsg(msg , 'success' , notyLayout , DEFAULT_TIMOUT , callback);
}

function error(msg,notyLayout) {
	if(notyLayout === undefined) {
		notyLayout = DEFAULT_NOTY_LAYOUT;
	}
	genMsg(msg , 'error' , notyLayout , DEFAULT_TIMOUT);
}


function loading(msg){
	genMsg(msg , 'loading' , DEFAULT_NOTY_LAYOUT , 0);
}


function confirm(msg, okCallback) {
	sweetAlert({
		title: msg,
		type: "warning",
		showCancelButton: true,
		confirmButtonColor: "#DD6B55",
		confirmButtonText: "确定",
		cancelButtonText:'取消',
		closeOnConfirm: true
	}, function () {
		if (okCallback != null && okCallback != undefined) {
			okCallback();
		}
	});
}

//
//var isShow = false;
//function confirm(msg, okCallback, cancelCallback) {
//	if(isShow) {
//		return;
//	}
//	noty({
//		text: msg,
//		layout: 'center',
//		type: 'confirmation',
//		dismissQueue: false,
//		killer: true,
//		maxVisible: 1,
//		force: false,
//		theme: 'defaultTheme',
//		timeout:0,
//		buttons: [{
//			addClass: 'btn btn-sm btn-primary',
//			text: '确定',
//			onClick: function($noty) {
//				$noty.close();
//				if (okCallback != null && okCallback != undefined) {
//					okCallback();
//				}
//			}
//		},
//			{
//				addClass: 'btn btn-sm btn-danger',
//				text: '取消',
//				onClick: function($noty) {
//					$noty.close();
//					if (cancelCallback != null && cancelCallback != undefined) {
//						cancelCallback();
//					}
//				}
//			}],
//		callback:{
//			onShow:function() {
//				isShow = true;
//			},
//			onClose:function() {
//				isShow = false;
//			}
//		}
//	});
//}
